package org.example.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.common.R;
import org.example.pojo.entity.WmNews;
import org.example.pojo.vo.WmNewsVo;
import org.example.pojo.vo.WmNewsVoPage;
import org.example.service.WmNewsService;
import org.example.mapper.WmNewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 翟旦旦
 * @description 针对表【wm_news(自媒体图文内容信息表)】的数据库操作Service实现
 
 */
@Service
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews>
        implements WmNewsService {

    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Override
    public Page<WmNewsVoPage> getArticleList(Integer pages, Integer size, String title, String name, String startTime, String endTime) {
        List<WmNewsVoPage> wmNewsVoPage = wmNewsMapper.getArticleList(title, name, startTime, endTime);
        Page page = new Page(pages, size);
        // 当前页第一条数据在List中的位置
        int start = (int) ((page.getCurrent() - 1) * page.getSize());
        // 当前页最后一条数据在List中的位置
        int end = (int) ((start + page.getSize()) > wmNewsVoPage.size() ? wmNewsVoPage.size() : (page.getSize() * page.getCurrent()));
        page.setRecords(new ArrayList<>());
        page.setTotal(wmNewsVoPage.size());
        if (page.getSize() * (page.getCurrent() - 1) <= page.getTotal()) {
            // 分隔列表 当前页存在数据时 设置
            page.setRecords(wmNewsVoPage.subList(start, end));
        }

        return page;

    }
}




